package com.example;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created with IntelliJ IDEA.
 * User: fransvanderlek
 * Date: 6/16/12
 * Time: 12:59 AM
 * To change this template use File | Settings | File Templates.
 */
public class BufferedStreamPipe {

    private InputStream input;
    private OutputStream output;
    private byte[] buffer ;

    public BufferedStreamPipe(InputStream input, OutputStream output, int chunckSize) {
        this.input = input;
        this.output = output;
        this.buffer = new byte[chunckSize];
    }

    public int transferChunck() throws IOException {

        int len = input.read(buffer) ;

        if ( len !=-1)
        {
            output.write(buffer, 0, len);
            output.flush();
        }
        else
        {
            input.close();
            output.close();
        }

        return len;

    }
}
